module.exports = {
  apps: [{
    // 应用基本配置
    name: 'dongning-backend',
    script: './Server/server.js',
    cwd: '/var/www/dongning',
    
    // 实例配置
    instances: 2, // 使用2个实例，适合小型服务器
    exec_mode: 'cluster',
    
    // 环境变量
    env: {
      NODE_ENV: 'development',
      PORT: 4000,
      MONGODB_URI: 'mongodb://localhost:27017/dongning'
    },
    env_production: {
      NODE_ENV: 'production',
      PORT: 4000,
      MONGODB_URI: 'mongodb://dongningServer:wangle321@localhost:27017/dongningServer',
      WECHAT_APP_ID: 'wxd4a47c612b973986',
      WECHAT_APP_SECRET: '0c65b990f85d15facf86661fd26239db',
      JWT_SECRET: 'DongNing2024!@#$%^&*()_+SecretKey_Production_Environment',
      CORS_ORIGIN: 'https://home.dongning.fun',
      DOMAIN: 'home.dongning.fun',
      PROTOCOL: 'https'
    },
    
    // 日志配置
    log_file: '/var/log/dongning/combined.log',
    out_file: '/var/log/dongning/out.log',
    error_file: '/var/log/dongning/error.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    merge_logs: true,
    
    // 进程管理
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    
    // 启动配置
    min_uptime: '10s',
    max_restarts: 10,
    restart_delay: 4000,
    
    // 健康检查
    health_check_grace_period: 3000,
    
    // 其他配置
    ignore_watch: [
      'node_modules',
      'File',
      'logs',
      '.git'
    ],
    
    // 优雅关闭
    kill_timeout: 5000,
    listen_timeout: 3000,
    
    // 源码映射支持
    source_map_support: true,
    
    // 实例变量
    instance_var: 'INSTANCE_ID'
  }],

  // 部署配置
  deploy: {
    production: {
      user: 'root',
      host: '39.96.171.44',
      ref: 'origin/master',
      repo: 'https://gitee.com/wangxudongheu/dong-ning-wechat-program-server.git',
      path: '/var/www/dongning',
      'pre-deploy-local': '',
      'post-deploy': 'npm install --production && pm2 reload ecosystem.config.js --env production',
      'pre-setup': 'mkdir -p /var/www/dongning && mkdir -p /var/log/dongning'
    }
  }
};